#!/usr/bin/perl -w
use Crypt::Lite;

########
# NOTE #
#########################################################
# Alot of variables you see in this code are defined in #
# the config file!                                      #
#########################################################
do "config";

# might wanna move around this password crap eventually....
sub comparepass { no warnings;

	my $crypt = Crypt::Lite->new( debug => 0 );
	my $password = $crypt->decrypt($password, 'passcheck');
	if ($checkpass eq $password) {
			              &menu;
				     }
	elsif ($checkpass ne $password) {
	print "\nSorry, but the password you entered doesnt seem to match the password given at install...";
	print "\n\n";
	print "Try again? (y/n):";
	$yesorno = <STDIN>;
	chomp $yesorno;
		warnings;
		exit;
		unless ($yesorno eq "y") {
			print "\n\n";
			print "Please enter the password for this datefile: ";
			$checkpass = <STDIN>;
			chomp $checkpass;
        		&comparepass;
				     };
		
	};
};

no warnings; 
if ($passprot == 1) {
	print "Please enter the password for this datefile: ";
	$checkpass = <STDIN>;
	chomp $checkpass;
        &comparepass;
		     };

warnings;
# yay for our new logging routines!
$logfile = "log.txt";
$time = localtime(time);

# Menu subroutine
sub menu {
print "\n";
print "\n";
print "Welcome to P-Date $version!\n";
print "\n";
print "Menu\n";
print "-------\n";
print "\n";
print "1. Insert an event into your datefile.\n";
print "2. Print your date file to see what events are coming up.\n";
print "3. About\n";
print "\n";
print "What do you want to do?: ";
$what = <STDIN>;
chomp $what;

# This takes our stdin, and finds out what the user said, and 
# goes to the correct subrouting based on input (basic, i know)
if ($what eq "1") {&write;
	open (LOG, ">>$logfile");
	print LOG ("user prompted the insert sub on $time\n");
	close (LOG);
}
   elsif ($what eq "2") {&print;
	open (LOG, ">>$logfile");
	print LOG ("user prompted the print sub on $time\n");
	close (LOG);
}
   elsif ($what eq "3") {&about;
	open (LOG, ">>$logfile");
	print LOG ("user prompted the about sub on $time\n");
	close (LOG);
};
}

# the print routine, basic stuff. Prints our file we have 
# selected, with the correct new lines
sub print {
print "\n";
open (MYFILE, $file);
while (<MYFILE>) {
  chomp;
  print "$_\n";
}
close (MYFILE);
print "\n";


};

# This is the juicy part, it writes our two event and date 
# variables to the file we specified.
sub write {
  print "Date: ";
  $date = <STDIN>;
  chomp $date;
  print "Event: ";
  $event = <STDIN>;
  chomp $date;
  open (PF, ">>$file");
  print PF ("$date \- $event");
  close (PF);
  open (LOG, ">>$logfile");
  print LOG ("user inserted new event into datefile on $time\n");
  close (LOG);

};

sub about {

print "\n\n";
print "Pdate was started as a practice project for clayton thompson in 2006.\n\n";
print "You can email him at fiendskull9\@gmail.com\n\n";


};

# asks what you wanna do, go back, or close.
print 'Return to the main menu? (y/n): ';
$answer = <STDIN>;
chomp $answer;
if ($answer eq 'y') { &menu }
 elsif ($answer eq 'n') {

	print "\n";
	print "\-----------------------------------\n";
	print "Thank you for using P-Date $version!";
	print "\n\-----------------------------------\n";

	exit;
};
